﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace XChip_Common
{
    public class FileHelper
    {
        /// <summary>
        ///获取文件夹下所有文件，包含子文件夹文件
        /// </summary>
        /// <param name="srcPath"></param>
        public static void GetFiles(string srcPath,ref List<string> files)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录                
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (DateTime.Compare(i.CreationTime,DateTime.Now) < 0) //文件的创建时间与当天的时间相同。
                    {
                        continue;
                    }
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        
                        GetFiles(i.FullName, ref files);
                    }
                    else
                    {
                        files.Add(i.FullName);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string CheckFiles(string _path)
        {
            StringBuilder sb = new StringBuilder();
            var fList = File.ReadAllLines(_path);



            foreach (var item in fList)
            {
                //添加缺省
                if (string.IsNullOrEmpty(item) || item.StartsWith(@"/") || item.StartsWith(@"\") || item.StartsWith(@"[") || item.EndsWith(@"]")||item.StartsWith(@"#")||item.StartsWith(@"{")||item.StartsWith("}"))
                {
                    continue;
                }
                //判断有没有两个逗号
                var sCount = item.Split(new char[2] { ',', '，' }).Length;
                if (item.IndexOf(",") == -1 || item.LastIndexOf(",") == -1 || sCount == 2)
                {
                    sb.Append($"{item} 逗号缺省\r\n");
                    continue;
                }
                //判断有没有两个0x
                if (item.IndexOf("0x") == -1 || item.LastIndexOf("0x") == -1)
                {
                    sb.Append($"{item} 0x缺省\r\n");
                    continue;
                }

                var strlist = new List<string>();
                Regex ConnoteA = new Regex("(0x)[a-fA-F0-9]{0,4}");
                foreach (var itemr in ConnoteA.Matches(item))
                {
                    strlist.Add(itemr.ToString().Replace("0x", "").Replace(" ", "").ToLower());
                }
                if (strlist.Count == 1)
                {
                    sb.Append($"{item} 0x缺省\r\n");
                    continue;
                }
                if (strlist[1].Length == 1)
                {
                    sb.Append($"{item} {strlist[1]}缺省\r\n");

                }

            }

            return sb.ToString();
        }

        /// <summary>
        /// 根据文件a 插入文件b 中的内容
        /// </summary>
        /// <param name="path1">文件a</param>
        /// <param name="path2">文件b</param>
        /// <param name="term">条件</param>
        /// <returns></returns>
        public static string ComMergeFiles(List<string> AList, List<string> BList, string path,string term,string sterm) {
            //string path = string.Empty;
            //读取文件
            //List<string> AList = File.ReadAllLines(path1, Encoding.Default).ToList();
            //List<string> BList = File.ReadAllLines(path2, Encoding.Default).ToList();
            int aStartIndex = AList.FindIndex(c => c.Contains(term));
            if (aStartIndex == -1)
            {
                return string.Empty;
            }
            //查找com
            int bStartIndex = BList.FindIndex(c => c.Contains(sterm));
            int bendIndex = BList.FindIndex(c => c.Contains("}"));
            if (bStartIndex == -1)
            {
                return string.Empty;
            }
            if (bendIndex == -1)
            {
                BList.Add("}");
            }
            List<string> Btemp = new List<string>();
            BList.Add("\n");
            for (int i = bStartIndex; i < BList.Count(); i++)
            {
                Btemp.Add(BList[i]);
            }
            AList.InsertRange(aStartIndex, Btemp);
            File.WriteAllLines(path, AList, Encoding.Default);
            return path;

        }

        //处理只读文件
        public static string ReadTextFromFileWithReadOnlyMode(string filename)
        {
            string content;
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default))
                {
                    StringBuilder sb = new StringBuilder();
                    while (!sr.EndOfStream)
                    {
                        sb.AppendLine(sr.ReadLine() + "<br>");
                    }
                    content = sb.ToString();
                }
            }
            return content;
        }

    }
}
